package org.itstack;




import org.itstack.model.Order;
import org.itstack.model.aggregates.TreeRich;
import org.itstack.model.vo.TreeNode;
import org.itstack.model.vo.TreeRoot;

import java.util.Map;

public abstract class OrderEngineBase extends OrderEngineConfig implements OrderEngine {
    
    @Override
    public abstract OrderProcessResult process(Order order, TreeRich treeRich);
    
    protected TreeNode engineDecisionMaker(TreeRich treeRich, Order order) {
        TreeRoot treeRoot = treeRich.getTreeRoot();
        Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();
        
        // 规则树根ID
        Long rootNodeId = treeRoot.getTreeRootNodeId();
        TreeNode treeNodeInfo = treeNodeMap.get(rootNodeId);
        
        // 节点类型[NodeType]；1子叶、2果实
        while (treeNodeInfo.getNodeType().equals(1)) {
            String ruleKey = treeNodeInfo.getRuleKey();
            OrderLogicFilter logicFilter = logicFilterMap.get(ruleKey);
            String matterValue = logicFilter.matterValue(order);
            Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLinkList());
            treeNodeInfo = treeNodeMap.get(nextNode);
            
            System.out.println("订单决策引擎=>" + treeRoot.getTreeName() + 
                             " 订单ID：" + order.getId() + 
                             " 规则Key：" + ruleKey + 
                             " 决策值：" + matterValue);
        }
        return treeNodeInfo;
    }
}
